import {createRouter, createWebHistory} from 'vue-router'
import {getClientToken, getServerToken} from "@/utils/token.ts";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/server',
            redirect: '/server/login'
        },
        {
            path: '/server/login',
            component: function () {
                return import('@/views/server/Login.vue')
            }
        },
        {
            path: '/server/home',
            component: function () {
                return import('@/views/server/Home.vue')
            },
            redirect: '/server/home/problem-manage',
            children: [
                {
                    path: '/server/home/problem-manage',
                    component: function () {
                        return import('@/views/server/problem/ProblemManage.vue')
                    }
                },
                {
                    path: '/server/home/user-manage',
                    component: function () {
                        return import('@/views/server/user/UserManage.vue')
                    }
                }
            ]
        },
        {
            path: '/server/problem-edit',
            component: function () {
                return import('@/views/server/problem/ProblemEdit.vue')
            },
            redirect: '/server/problem-edit/content',
            children: [
                {
                    path: '/server/problem-edit/content',
                    component: function () {
                        return import('@/views/server/problem/ProblemContent.vue')
                    }
                },
                {
                    path: '/server/problem-edit/case',
                    component: function () {
                        return import('@/views/server/problem/ProblemCase.vue')
                    }
                },
                {
                    path: '/server/problem-edit/code',
                    component: function () {
                        return import('@/views/server/problem/ProblemCode.vue')
                    }
                }
            ]
        },

        {
            path: '/',
            redirect: '/client/password-login'
        },
        {
            path: '/client',
            redirect: '/client/home',
        },
        {
            path: '/client/sms-login',
            component: function () {
                return import('@/views/client/auth/SmsLogin.vue')
            }
        },
        {
            path: '/client/password-login',
            component: function () {
                return import('@/views/client/auth/PasswordLogin.vue')
            }
        },
        {
            path: '/client/home',
            component: function () {
                return import('@/views/client/Home.vue')
            },
            redirect: '/client/home/problem',
            children: [
                {
                    path: '/client/home/problem',
                    component: function () {
                        return import('@/views/client/problem/ProblemManage.vue')
                    }
                }
            ]
        },
        {
            path: '/client/problem/answer',
            component: function () {
                return import('@/views/client/problem/ProblemAnswer.vue')
            }
        },
        {
            path: '/client/user',
            component: function () {
                return import('@/views/client/user/UserInfo.vue')
            }
        }
    ],
})

router.beforeEach((to, from, next) => {
    if (to.path.startsWith('/server')) {
        if (getServerToken()) {
            if (to.path === '/server/login') {
                next('/server/home')
            } else {
                next()
            }
        } else {
            if (to.path === '/server/login') {
                next()  // 允许访问 /server/login，避免死循环
            } else {
                next('/server/login')  // 未登录，跳转到 /server/login
            }
        }
    } else if (to.path.startsWith('/client')) {
        if (getClientToken()) {
            if (to.path === '/client/sms-login' || to.path === '/client/password-login') {
                next('/client/home')
            } else {
                next()
            }
        } else {
            if (to.path === '/client/sms-login' || to.path === '/client/password-login') {
                next()
            } else {
                next('/client/password-login')
            }
        }
    } else {
        next()
    }
})

export default router
